package it.polimi.entitiesManagers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import it.polimi.authentication.ejb.usermanagement.Group;
import it.polimi.entities.Attraction;
import it.polimi.entities.Flight;
import it.polimi.entities.Hotel;
import it.polimi.entitiesDTO.AttractionDTO;

@Stateless
public class AttractionManagerBean implements AttractionMgr {

	@PersistenceContext
	private EntityManager em;

	@Resource
	private EJBContext context;

	@Override
	@RolesAllowed({ Group._EMPLOYEE })
	public void save(AttractionDTO attraction) {
		Attraction newAttraction = new Attraction(attraction);
		em.persist(newAttraction);
	}

	@Override
	public void update(AttractionDTO attraction) {
		em.merge(new Attraction(attraction));
	}

	@Override
	public AttractionDTO getAttractionDTO() {
		AttractionDTO attractionDTO = convertToDTO(getPrincipalAttraction());
		return attractionDTO;
	}

	public Attraction find(String name) {
		return em.find(Attraction.class, name);
	}

	public Attraction getPrincipalAttraction() {
		return find(getPrincipalName());
	}

	public String getPrincipalName() {
		return context.getCallerPrincipal().getName();
	}

	public Attraction getAttraction(String id) {
		return em
				.createNamedQuery(Attraction.FIND_ATTRACTION, Attraction.class)
				.setParameter("id", id).getSingleResult();
	}

	private AttractionDTO convertToDTO(Attraction attraction) {
		AttractionDTO attractionDTO = new AttractionDTO();
		attractionDTO.setCity(attraction.getCity());
		attractionDTO.setDescription(attraction.getDescription());
		attractionDTO.setDuration(attraction.getDuration());
		attractionDTO.setPrice(attraction.getPrice());
		attractionDTO.setSpots(attraction.getSpots());
		attractionDTO.setVisibility(attraction.getVisibility());
		return attractionDTO;
	}

	private List<Attraction> attractionByCity(String city) {
		return em.createNamedQuery(Attraction.FIND_ATTRACTION_BY_CITY, Attraction.class)
				.setParameter("city", city).getResultList();
	}

	@Override
	@RolesAllowed({ Group._EMPLOYEE })
	public List<String> getAttractionByCity(String city) {
		List<String> someAttraction = new ArrayList<String>();
		for (Attraction a : attractionByCity(city))
			someAttraction.add(a.toString());
		return someAttraction;
	}

	@Override
	public List<Integer> getAllId() {
		List<Integer> list = new ArrayList<Integer>();
		List<Attraction> attrList = em.createNamedQuery(Attraction.FIND_ALL, Attraction.class).getResultList();
		for(Attraction a: attrList){
			list.add((Integer)a.getId());
		}
		return list;
	}

	@Override
	public List<String> showAllAttractions() {
		List<String> list = new ArrayList<String>();
		List<Attraction> attrList = em.createNamedQuery(Attraction.FIND_ALL, Attraction.class).getResultList();
		for(Attraction a: attrList){
			if(a.getVisibility())
			list.add(a.toString());
		}
		return list;
	}

	@Override
	public void remove(int id) {
		Attraction a = em.createNamedQuery(Attraction.FIND_ATTRACTION, Attraction.class).setParameter("id", id).getSingleResult();
		em.remove(a);
	}

}
